package com.xiaorun.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaorun.portal.constant.WebConst;
import com.xiaorun.portal.dao.NewsMapper;
import com.xiaorun.portal.modal.Bo.Query;
import com.xiaorun.portal.modal.Vo.News;
import com.xiaorun.portal.service.INewsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by BlueT on 2018/3/3.
 */
@Service
@Slf4j
public class NewsServiceImpl implements INewsService {

    @Autowired
    private NewsMapper newsMapper;

    @Override
    public PageInfo<News> list(int pageNum, int pageSize, Map<String, Object> params) {
        PageHelper.startPage(pageNum, pageSize);
        Query query = new Query(params != null ? params: new HashMap<>());
        List<News> newsList = newsMapper.list(query);

        for(News news : newsList){
            if(new Date().after(news.getApplyEndDate())){
                news.setApplyStatus(WebConst.END);
            }else{
                news.setApplyStatus(WebConst.APPLYING);
            }
        }

        PageInfo<News> pageInfo = new PageInfo(newsList);
        return pageInfo;
    }

    @Override
    public int save(News news){
        int result;
        if(news.getId() != null){
            result = newsMapper.update(news);
        }else{
            result = newsMapper.save(news);
        }
        return result;
    }

    @Override
    public News get(int id) {
        return newsMapper.get(id);
    }

    @Override
    public int remove(int id) {
        return newsMapper.remove(id);
    }

    @Override
    public int countTotal(Map<String, Object> params) {
        Query query = new Query(params != null ? params: new HashMap<>());
       return newsMapper.countTotal(query);
    }
}
